<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {
	/* 每页留言数 */
	const NUM_PER_PAGE = 5;
	/**
	 * 留言板的主页
	 */
	public function index($page_ind=0)
	{
		$this->load->model("Site_model", "m");
		$page_ind = (int)$page_ind;
		$msg = $this->m->get_messages($page_ind, self::NUM_PER_PAGE);

		$this->load->library('pagination');
		$config['base_url'] = site_url('site/index');
		$config['total_rows'] = $this->m->get_num();
		$config['per_page'] = self::NUM_PER_PAGE;

		$this->pagination->initialize($config);
		$links = $this->pagination->create_links();

		$data = array(
			"msg" => $msg,
			"page_title" => "主页",
			"page_link" => $links
		);

		$this->load->view("templates/header", $data);
		$this->load->view("site_index");
		$this->load->view("templates/footer");
	}
	public function post() {
		$this->load->library("form_validation");
		$this->form_validation->set_rules('name', '名字', 'trim|required|xss_clean');
		$this->form_validation->set_rules('email', '邮箱', 'trim|required|valid_email');
		$this->form_validation->set_rules('content', '内容', 'trim|required|xss_clean');
		if ($this->form_validation->run()) {
			$this->load->model("Site_model", "m");
			$data["name"] = $this->input->post("name");
			$data["email"] = $this->input->post("email");
			$data["content"] = $this->input->post("content");
			$this->m->add_message($data);
		}
		$this->index();
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */